package model

import (
	"fmt"
)

// 声明customer结构体，表示一个客户信息
type Customer struct {
	Id int
	Name string
	Gender string
	Age int
	Phone string
	Email string
}

func NewCustomer(Id int, Name string, Gender string, Age int, Phone string, Email string) Customer {
	return Customer {
		Id: Id,
		Name: Name,
		Gender: Gender,
		Age: Age,
		Phone: Phone,
		Email: Email,
	}
}

// 第二种创建Customer实例方法，不带Id
func NewCustomer2(Name string, Gender string, Age int, Phone string, Email string) Customer {
	return Customer {
		Name: Name,
		Gender: Gender,
		Age: Age,
		Phone: Phone,
		Email: Email,
	}
}

func (this Customer) GetInfo() string {
	info := fmt.Sprintf("%v\t%v\t%v\t%v\t\n", this.Id, this.Name, this.Gender, this.Age, this.Phone, this.Email)
	return info
}

// 根据id删除客户(从切片中删除)
func (this *CustomerService) Delete(id int) bool {

	index := this.FindById(id)

	// 如果index == -1， 说明没有这个客户
	if index == -1 {
		return false
	}

	// 如何从切片中删除一个元素
	this.customers = append(this.customers[:index], this.customers[index+1:]...)
	return true

}

// 根据id查找客户在切片中对应下标，如果没有该客户，返回-1
func (this *CustomerService) FindById(id int) int {

	index := -1
	// 遍历this.customers 切片
	for i := 0; i < len(this.customers); i++ {

		if this.customers[i].Id == Id {

			index = i

		}

	}

}